package com.demo.consumer;

import com.demo.exetion.MainExection;
import com.demo.model.MovieOrder;
import com.demo.service.MovieOrderService;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;

@Component
@RocketMQMessageListener(consumerGroup = "order_pay_order",topic = "order_pay_order")
public class OrderPayOrderConsumer implements RocketMQListener<BigInteger> {
    @Resource
    private MovieOrderService movieOrderService;
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onMessage(BigInteger bigInteger) {
        System.out.println(bigInteger);
        String queryOrder = movieOrderService.queryOrder(bigInteger);
        if (queryOrder.equals("TRADE_SUCCESS")){
            MovieOrder movieOrderServiceById = movieOrderService.getById(bigInteger);
            if (movieOrderServiceById.getOrderStart()!=3){
                movieOrderServiceById.setOrderStart(3);
                boolean b = movieOrderService.updateById(movieOrderServiceById);
                if (!b)throw new MainExection(500,"消费者修改订单状态为3失败");
            }
        }
    }
}
